#include "mycoin.h"
#include<QPixmap>
#include<QString>
#include<QDebug>

MyCoin::MyCoin(QString btnImg){
    QPixmap pix;
    bool res=pix.load(btnImg);
    if(!res){
        QString str=QString("图片%1加载失败").arg(btnImg);
        qDebug()<<str;
        return ;
    }
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    timer1=new QTimer(this);
    timer2=new QTimer(this);

    connect(timer1,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/res/Coin000%1.png").arg(this->min_N++);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->min_N>this->max_N){
            this->min_N=1;
            isAnimation=false;
            timer1->stop();
        }
    });

    connect(timer2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/res/Coin000%1.png").arg(this->max_N--);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->min_N>this->max_N){
            this->max_N=8;
            isAnimation=false;
            timer2->stop();
        }
    });
}

void MyCoin::changeFlag(){
    if(this->flag){
        isAnimation=true;
        timer1->start(30);
        this->flag=false;
    }
    else{
        isAnimation=true;
        timer2->start(30);
        this->flag=true;
    }
}

// 胜利过后，不可以点击按钮，其实也就是覆盖了父类按钮点击事件
void MyCoin::mousePressEvent(QMouseEvent *e){
    if(isAnimation||this->isWin){
        return;
    }
    return QPushButton::mousePressEvent(e);
}
